package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.utils.value.ValueChecker;
import com.jvyou.app.ds.domain.dto.LessonStudentDto;
import com.jvyou.app.ds.domain.entity.LessonStudent;
import com.jvyou.app.ds.service.LessonStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 课时学员关联表(LessonStudent)表控制层
 *
 * @author 橘柚
 * @since 2025-04-17 17:08:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lessonStudent")
public class LessonStudentController {

    private final LessonStudentService lessonStudentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("ds:lessonStudent:get")
    public Result<LessonStudent> selectOne(@PathVariable Long id) {
        return Result.success(lessonStudentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param lessonStudent 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("ds:lessonStudent:insert")
    public Result<LessonStudent> insert(@RequestBody LessonStudent lessonStudent) {
        boolean success = lessonStudentService.save(lessonStudent);
        return success ? Result.success(lessonStudent) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param lessonStudent 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("ds:lessonStudent:update")
    public Result<Boolean> updateById(@RequestBody LessonStudent lessonStudent) {
        boolean success = lessonStudentService.updateById(lessonStudent);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("ds:lessonStudent:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = lessonStudentService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    /**
     * 签到
     */
    @PostMapping("/signIn")
    public Result<Boolean> signIn(@RequestBody LessonStudentDto lessonStudentDto) {
        ValueChecker.checkNotNull(lessonStudentDto.getLessonId(), "课程编码不能为空");
        return Result.success(lessonStudentService.signIn(lessonStudentDto.getLessonId()));
    }

    /**
     * 预约
     */
    @PostMapping("/reserve")
    public Result<Boolean> reserve(@RequestBody LessonStudentDto lessonStudentDto) {
        ValueChecker.checkNotNull(lessonStudentDto.getLessonId(), "课程编码不能为空");
        return Result.success(lessonStudentService.reserve(lessonStudentDto.getLessonId()));
    }

}

